#include "StdAfx.h"
#include "Log.h"


int CLog::Format( char* buffer, int size, const char* fmt, ... )
{
	if (buffer == NULL) return 0;

	va_list args;
	va_start(args, fmt);
	int n = vsprintf_s(buffer, size-1, fmt, args);
	va_end(args);
	return n;
}


CScriptLog& CScriptLog::Instance()
{
	static CScriptLog logger;
	return logger;
}

void CScriptLog::InitLogger( CEdit* edit )
{
	if (edit != NULL)
		m_edtLog = edit;
}

int CScriptLog::Log( const char* fmt, ...)
{
	char buffer[2048] = {0};
	va_list args;
	va_start(args, fmt);
	int n = vsprintf_s(buffer, sizeof(buffer)-1, fmt, args);
	va_end(args);
	
	AppendLog(buffer);
	return n;
}

int CScriptLog::LogWarning( const char* fmt, ... )
{
	char buffer[2048] = {0};
	va_list args;
	va_start(args, fmt);
	int n = vsprintf_s(buffer, sizeof(buffer)-1, fmt, args);
	va_end(args);

	AppendLog(buffer);

	return n;
}

int CScriptLog::LogError( const char* fmt, ... )
{
	char buffer[2048] = {0};
	va_list args;
	va_start(args, fmt);
	int n = vsprintf_s(buffer, sizeof(buffer)-1, fmt, args);
	va_end(args);

	AppendLog(buffer);

	return n;
}

void CScriptLog::AppendLog( const char* strMsg )
{
	if (strMsg != NULL && m_edtLog != NULL && IsWindow(m_edtLog->m_hWnd))
	{
		CString str;
		m_edtLog->GetWindowText(str);
		m_edtLog->SetWindowText(strMsg + str);
	}
	
}

